package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Tsuser;
import com.chezhujie.www.service.HomeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by 汤伟杰 on 2017/5/14.
 */
//@Controller
@RequestMapping("/admin")
public class HomeController {
    @Autowired
    private HomeService homeService;


    @RequestMapping({"/home.do","/index.do",""})
    public String home(HttpServletRequest req, HttpServletResponse resp,
                        ModelMap model) {
        Tsuser user =  (Tsuser) req.getSession().getAttribute("user");

        if(user==null){
            return "admin/login";
        }
        return "admin/home";
    }
    @RequestMapping(value = "/login.do",method = RequestMethod.GET)
    public void index(HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) throws IOException {
        StringBuffer sb = new StringBuffer();
        sb.append("<!DOCTYPE html>\n");
        sb.append("<html lang=\"en\">\n");
        sb.append("<head>\n");
        sb.append("<meta charset=\"UTF-8\">\n");
        sb.append("<title>Title</title>\n" );
        sb.append("</head>\n");
        sb.append("<body bgcolor=\"#FFFFFF\" text=\"#000000\">\n");
        sb.append("<script language='javascript'>top.location.href = '");
        sb.append(req.getContextPath()+"/admin/index.do");
        sb.append("'</script>\n");
        sb.append("</body>\n");
        sb.append("</html>");

        resp.getWriter().print(sb.toString());
    }

    @RequestMapping(value = "logout.do")
    public String logout(HttpServletRequest req, HttpServletResponse resp,
                         ModelMap model) {
        HttpSession session = req.getSession();
        session.removeAttribute("user");
        model.addAttribute("msg","退出登陆");
        return "redirect:index.do";
    }
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    public String login(String username,
                        String password,
                        String vcode,
                        HttpServletRequest req, HttpServletResponse resp,
                        ModelMap model) {

        //登录检查：检查验证码
        String session_vcode = String.valueOf(req.getSession().getAttribute("rand"));
        if(StringUtils.isNotBlank(session_vcode)&&!"null".equals(session_vcode.toLowerCase())) {//为空跳过验证码
            if(StringUtils.isNotBlank(vcode)){
                if(!session_vcode.equals(vcode.trim())){
                    model.addAttribute("username", username);
                    model.addAttribute("password", password);
                    model.addAttribute("vcode", vcode);
                    model.addAttribute("msg", "验证码校验失败");
                    return "admin/login";
                }else{
                    //验证码校验成功
                }
            }else{
                model.addAttribute("username", username);
                model.addAttribute("password", password);
                model.addAttribute("vcode", vcode);
                model.addAttribute("msg", "验证码不能为空");
                return "admin/login";
            }

        }
        //登录检查：检查用户名和密码
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
            if (homeService.checkLogin(username.trim(), password.trim(), req)) {
                return "redirect:index.do";
            } else {
                model.addAttribute("username", username);
                model.addAttribute("password", password);
                model.addAttribute("vcode", vcode);
                model.addAttribute("msg", "用户名或密码出错");
            }

        }else if(StringUtils.isNotBlank(username) || StringUtils.isNotBlank(password)){
            model.addAttribute("username", username);
            model.addAttribute("password", password);
            model.addAttribute("vcode", vcode);
            model.addAttribute("msg", "用户名或密码不能为空");
        }

        return "admin/login";
    }
    /** api测试页面 */
    @RequestMapping("/apiTest.do")
    public String apiTest(HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model) throws Exception {
        return "admin/apiTest";

    }

}
